[python]收納一些常見問題——更新於20200615

python經驗

1.python 五種下劃線的含義
https://zhuanlan.zhihu.com/p/36173202

數據獲取

1>一個標籤中有多行數據, 如何分行獲取
我遇到的情況是這樣的:
紅線是目標數據
源碼是:

<p>
	杭州 餘杭區 倉前
	<em class="vline"></em>
	1-3年
	<em class="vline"></em>
	本科
</p>

解決方法

# selenium獲取這個p標籤的源碼,然後split成list
info_ls = chrome.find_element_by_xpath('//div[@class="job-banner"]//p').get_attribute('innerHTML').split('<em class="vline"></em>')
item['City'] = info_ls[0]	# 城市
item['eduLevel'] = info_ls[1]	# 學歷
item['workingExp'] = info_ls[2]		# 工作經驗

Pyspider

1>安裝pyspider過程出錯:ERROR: Command errored out with exit status 10: python setup.py egg_info Check…
https://blog.csdn.net/weixin_43810415/article/details/99694315

2>pyspider all運行出錯:①SyntaxError: invalid syntax,② - Deprecated option ‘domaincontroller’: use 'http_au
https://blog.csdn.net/u012424313/article/details/89511520

3>運行時出現: ValueError: Invalid configuration
解決方法: pip install wsgidav==2.4.1

4>運行時卡在result_worker starting…
錯誤如下:

(venv1) D:\Fire\PycharmProject\pyspider\test1&gt;pyspider
c:\users\xxx\pycharmprojects\untitled1\venv1\lib\site-packages\pyspider\libs\utils.py:196: FutureWarning: timeout is not supported on you
r platform.
  warnings.warn(&quot;timeout is not supported on your platform.&quot;, FutureWarning)
[W 191028 21:30:05 run:413] phantomjs not found, continue running without it.
[I 191028 21:30:07 result_worker:49] result_worker starting...

解決方法:
下載phantomjs, 然後將phantomjs.exe拖到python根目錄, 重新運行即可
如果還是不行,請參考:https://blog.csdn.net/qq_35167821/article/details/89162394

5. 在實際的調試中發現pyspider的Web預覽界面只有一點非常小
這篇文章中的第3個:
https://www.jianshu.com/p/7bff6fd4dc1b


數據儲存

1>MongoDB讓數據具有過期時間
主要使用pymongo庫中的createIndex()方法, 其中有個expireAfterSeconds的參數, 作用是指定一個以秒爲單位的數值,可以用來創建一個具有過期時間的索引, 這樣之後寫入的集合就可以擁有過期時間
詳情參考: https://www.runoob.com/mongodb/mongodb-indexing.html
注:指定的索引寫入時必須爲datetime格式, 否則不會自動刪除


其他問題

1>解決windows下 cd 無法切換盤符目錄
https://blog.csdn.net/kakuma_chen/article/details/71173243

2>關於解決’\u’開頭的字符串轉中文的方法
https://www.cnblogs.com/hahaxzy9500/p/7685955.html

3>修改jupyter notebook啓動的虛擬環境
合併查看一下兩篇文章:
https://blog.csdn.net/hao5335156/article/details/81165727
https://blog.csdn.net/weixin_41813895/article/details/84750990

4.python爬蟲 隨機UA庫
https://blog.csdn.net/qq_18525247/article/details/81355397

5. 如何獲取頁面上單選按鈕的值
jq方法:$('input:radio:checked').val();
原文鏈接:https://blog.csdn.net/z132411/article/details/105090806

6. 使用selenium時,如何用xpath取到iframe裏的元素
selenium獲取page_source時不包括內嵌iframe的源碼,因此想要取到內嵌iframe的源碼需要先切換iframe,再重新獲取一次page_source
靈感來源:https://blog.csdn.net/qq_35768238/article/details/103913249

7. xpath高級用法

表達式 解釋
//*[contains(@class, 'text')] 選取元素,其屬性包含某些字段
//div[contains(string(), '測試字段')] 選取某個內容包含文本測試字段的div標籤
//input[@value='1']/following-sibling::input[@value='0'] 選取當前節點之後的所有同級節點
這裏是選取value=0input的兄弟標籤,其value=1

參考文章:

  1. XPath定位中and、or、not、contains、starts-with和string(.)用法
  2. 定位元素的父(parent::)、兄弟(following-sibling::、preceding-sibling::)節點

8. 在Win10上用pyinstaller打包後,無法在Win7中打開
兩種解決方法:

  1. 簡單粗暴 – 在win7上用pyinstaller打包
  2. 下載VC++ redis 2015安裝在Win10和Win7上,再用pyinstaller打包一次,即可在Win7中運行

參考文章: Pyinstaller 打包發佈經驗總結

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章